package com.example.demo.controller;

import com.example.demo.entity.RolePerm;
import com.example.demo.entity1.Perm;
import com.example.demo.entity.Role;
import com.example.demo.repository.RoleRepository;
import com.example.demo.repository.impl.TestImpl;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.*;
import java.util.List;

/**
 * rest controller
 * Created by Administrator on 2017/11/9.
 */
@RestController
public class HelloController {

    @Resource
    private RoleRepository roleRepository;
    @Resource
    @Qualifier("PermRepository")
    private com.example.demo.repository.PermRepository permRepository;
    @Resource
    @Qualifier("PermRepository1")
    private com.example.demo.repository1.PermRepository permRepository1;
    @Resource
    @Qualifier("TestImpl")
    private TestImpl testImpl;

    @RequestMapping("/testrepeat")
    public String testEntity() {

        List<Role> roleList = roleRepository.findRoleByName("ROLE_USER1");
        List<Perm> permList = permRepository.findPermByName("PERM_USER1");
        List<Perm> permList1 = permRepository1.findPermByName("PERM_USER1");
        List<Perm> permList2 = testImpl.queryPermByName();
        List<RolePerm> permList3 = testImpl.queryPermBySQLName();

        System.out.println(roleList.get(0).getClass().getName());
        System.out.println(permList.size());
        System.out.println(permList1.size());
        System.out.println(permList2.size());
        System.out.println(permList3.get(0).getRoleName());

        return "Hello World";
    }

    @RequestMapping("/getPath")
    public String getPath() throws IOException {

        String path = System.getProperty("java.class.path");
        int firstIndex = path.lastIndexOf(System.getProperty("path.separator")) + 1;
        int lastIndex = path.lastIndexOf(File.separator) + 1;
        path = path.substring(firstIndex, lastIndex);
        path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        System.out.println("path = " + path);

        InputStream stream = getClass().getClassLoader().getResourceAsStream("application.properties");
        File targetFile = new File("application.properties");
        FileUtils.copyInputStreamToFile(stream, targetFile);
        System.out.println(targetFile.getAbsoluteFile());
        return targetFile.getAbsolutePath();
    }

    @RequestMapping("/hello")
    public String hello() {
        return "Hello World";
    }

    @RequestMapping("/testRepository")
    public String testRepository(){
        List<Role> roleList = roleRepository.findRoleByName("ROLE_USER");
        return "size : " + roleList.size();
    }

    public void inputstreamtofile(InputStream ins,File file) throws IOException {
        OutputStream os = new FileOutputStream(file);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
    }
}
